package com.caiyi.financial.nirvana.sms.gateway.sgip.message.body;

import com.caiyi.financial.nirvana.sms.gateway.cmpp.CMPPMessageTool;
import com.caiyi.financial.nirvana.sms.gateway.cmpp.CharsetInfo;
import com.caiyi.financial.nirvana.sms.gateway.sgip.message.SGIPBody;
import io.netty.buffer.ByteBuf;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 上行短信消息体
 */
public class SGIPDeliver extends SGIPBody {

    private static final Logger logger = LoggerFactory.getLogger(SGIPDeliver.class);

    private String userNumber;    //发送短消息的用户手机号，手机号码前加“86”国别标志
    private String spNumber;    //SP的接入号码
    private byte tpPid;
    private byte tpUdhi;
    private CharsetInfo messageFormat;    //短消息的编码格式
    private int messageLength;
    private String messageContent;
    private String reserved;

    @Override
    public int encode(ByteBuf buffer) {
        throw new UnsupportedOperationException("not need");
    }

    /**
     * 解码
     */
    @Override
    public SGIPBody decode(ByteBuf buffer) {

        //21字节用户手机号
        byte[] temp = new byte[21];
        buffer.readBytes(temp);
        temp = CMPPMessageTool.trimStringBufferRightZeros(temp);
        this.userNumber = new String(temp).intern();

        //21字节SP接入号码
        temp = new byte[21];
        buffer.readBytes(temp);
        temp = CMPPMessageTool.trimStringBufferRightZeros(temp);
        this.spNumber = new String(temp).intern();

        //TP_pid
        this.tpPid = buffer.readByte();

        //TP_udhi
        this.tpUdhi = buffer.readByte();

        //格式
        this.messageFormat = CharsetInfo.fromCMPPFormat(buffer.readByte());

        //消息长度
        this.messageLength = buffer.readInt();

        temp = new byte[messageLength];
        buffer.readBytes(temp);
        if (tpUdhi == 1) {
            byte[] temp2 = ArrayUtils.subarray(temp, 6, temp.length);
            this.messageContent = messageFormat.decode(temp2);
        } else {
            this.messageContent = messageFormat.decode(temp);
        }

        //保留项
        temp = new byte[8];
        buffer.readBytes(temp);
        reserved = new String(temp);

        return this;
    }

    public String getUserNumber() {
        if (this.userNumber.startsWith("86")) {
            this.userNumber = this.userNumber.substring(2);
        }
        return userNumber;
    }

    public void setUserNumber(String userNumber) {
        this.userNumber = userNumber;
    }

    public String getSpNumber() {
        return spNumber;
    }

    public void setSpNumber(String spNumber) {
        this.spNumber = spNumber;
    }

    /**
     * @return the tpPid
     */
    public byte getTpPid() {
        return tpPid;
    }

    /**
     * @param tpPid the tpPid to set
     */
    public void setTpPid(byte tpPid) {
        this.tpPid = tpPid;
    }

    /**
     * @return the tpUdhi
     */
    public byte getTpUdhi() {
        return tpUdhi;
    }

    /**
     * @param tpUdhi the tpUdhi to set
     */
    public void setTpUdhi(byte tpUdhi) {
        this.tpUdhi = tpUdhi;
    }

    /**
     * @return the messageFormat
     */
    public CharsetInfo getMessageFormat() {
        return messageFormat;
    }


    /**
     * @param messageFormat the messageFormat to set
     */
    public void setMessageFormat(CharsetInfo messageFormat) {
        this.messageFormat = messageFormat;
    }

    /**
     * @return the messageLength
     */
    public int getMessageLength() {
        return messageLength;
    }


    /**
     * @param messageLength the messageLength to set
     */
    public void setMessageLength(byte messageLength) {
        this.messageLength = messageLength;
    }

    /**
     * @param messageContent the messageContent to set
     */
    public void setMessageContent(String messageContent) {
        this.messageContent = messageContent;
    }

    public String getMessageContent() {
        return messageContent;
    }

    public String getReserved() {
        return reserved;
    }

    @Override
    public String toString() {
        return "SGIPDeliver [logger=" + logger + ", userNumber=" + userNumber + ", spNumber=" + spNumber
                + ", tpPid=" + tpPid + ", tpUdhi=" + tpUdhi + ", messageFormat=" + messageFormat
                + ", messageLength=" + messageLength
                + ", messageContent=" + messageContent + ", reserved=" + reserved + "]";
    }
}
